const logModel = require('../models/logModel')

module.exports = function (message, type = 'info') {
	const colors = {
		info: 37,
		warn: 33,
		error: 31,
		fatal: 35,
		success: 32
	}

	const now = new Date()
	const format = `${now.getDate()}.${now.getMonth() + 1}.${now.getFullYear()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`

	if (type !== 'info') {
		try {
			logModel.create({ type, message })
		} catch (e) {
			this.constructor('Не удалось записать лог в базу данных', 'error')
		}
	}

	console.log(
		`\u001b[${colors[type]}m` +
		`[${format}] | ${type} | >_ ${message}`
		+ `\u001b[${colors['info']}m`
	)
}